
from . import tushare_sdk as datasdk
# import db
# import time
# from datetime import datetime, timedelta
# import pandas as pd
# from . import tushare_sdk as datasdk
#
#
# DATE_FORMAT = "%Y%m%d"
# DEFAULT_DATA_LONG = 30
# FRE_GRAP = 1
#
# d2id = lambda x: int(x.strftime(DATE_FORMAT))
# id2d = lambda x: datetime.strptime(str(x), DATE_FORMAT)
#
# def _gen_start_and_end(ts_code):
#     today = datetime.today()
#     last_data = db.DailyTB.select().where(ts_code=ts_code).order_by("trade_date desc").first()
#     start_date = (today - timedelta(days=DEFAULT_DATA_LONG) if not last_data else
#                   id2d(last_data["trade_date"]) + timedelta(days=1))
#     end_date = today - timedelta(days=1)
#     if start_date > end_date:
#         return None, None
#     return str(d2id(start_date)), str(d2id(end_date))
#
# def get_daily(ts_code, days=30):
#     """取日交易历史数据"""
#     # 补全缺失的数据
#     start_date, end_date = _gen_start_and_end(ts_code)
#     if start_date and end_date:
#         miss_daily = datasdk.get_daily(ts_code, start_date, end_date)
#         db.DailyTB.add_batch(miss_daily.to_dict(orient='records'))
#         time.sleep(FRE_GRAP)
#     # 取数据
#     end = datetime.today()
#     start_date = d2id(end - timedelta(days=days))
#     daily_data = db.DailyTB.find(trade_date = (">", start_date), ts_code=ts_code)
#     return daily_data
#
# def get_stocks():
#     # 查看数据库是否存在数据
#     today = datetime.today()
#     stocks = db.StockTB.find(cdate=d2id(today))
#     if stocks:
#         return stocks
#     if not stocks:
#         # 下载最新数据
#         stocks = datasdk.get_all_stock()
#         time.sleep(FRE_GRAP)
#         if not stocks.empty:
#             db.StockTB.rm()
#             stocks["cdate"] = d2id(today)
#             db.StockTB.add_batch(stocks.to_dict(orient='records'))
#         return stocks.to_dict(orient="records")
#



